//
//  InfosXmlParser.m
//  xKey Interface
//
//  Created by Guillaume SOURDRILLE on 16/04/13.
//  Copyright (c) 2013 Edenyorke. All rights reserved.
//

#import "InfosXmlParser.h"
#import "URLConnection.h"
#import "Data.h"
#import "Infos.h"

@implementation InfosXmlParser


-(NSMutableArray*) parseXmlFromUrl: (NSURL*) url{
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    donnes = [[NSMutableArray alloc] init];
    [xmlParser setDelegate:self];
    [xmlParser parse];
    return donnes;
    
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;
    if ([elementName isEqualToString:@"ITEM"])
    {
        NSLog(@"element ITEM trouvé: %@", elementName);
        items = [[Infos alloc] init];
        currentName = [[NSMutableString alloc] init];
        currentName = [attributeDict objectForKey:@"NAME"] ;
        currentValue = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"ITEM"]) {
        
        [items setKey:currentName];
        [items setValue:[currentValue stringByReplacingOccurrencesOfString:@"\n" withString:@""]];
        [donnes addObject:items];
        
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"ITEM"])
    {
       
        [currentValue appendString:string];
    }
}

@end
